package com.fan.request;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.Set;

public class Servlet3 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          request.setCharacterEncoding("UTF-8");
          //1.根据参数名，获取一个参数值
        String usename = request.getParameter("usename");
        System.out.println("获取的请求参数username："+usename);
        //2.根据参数名，获取多个参数值：比如说注册时候的兴趣爱好的复选框
        String[] hobbies = request.getParameterValues("hobby");
        for (String hobby : hobbies) {
            System.out.println(hobby);
        }
        //3.获取所有的请求参数
        //getParameterMap()获取所有请求参数，请求参数的参数名就是map的key，请求参数的参数
        //值就是map的value
        Map<String, String[]> parameterMap = request.getParameterMap();
        //遍历每一个请求参数
        Set<Map.Entry<String, String[]>> entries = parameterMap.entrySet();
        for (Map.Entry<String, String[]> entry : entries) {
            String key = entry.getKey();
            String[] value = entry.getValue();
            for (String s : value) {
                System.out.println("参数名"+key+",参数值："+s);
            }
        }
    }
}
